<!doctype html>
<html lang="en">

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<title>Todo List</title>
	<link rel="stylesheet" href="/css/base.css">
	<link rel="stylesheet" href="/css/index.css">
	<link rel="stylesheet" href="/nprogress/nprogress.css">
</head>

<body>
	<section class="todoapp">
		<header class="header">
			<h1>todos</h1>
			<input class="new-todo" placeholder="What needs to be done?" autofocus>
		</header>
		<!-- This section should be hidden by default and shown when there are todos -->
		<section class="main">
			<input class="toggle-all" type="checkbox">
			<label for="toggle-all">Mark all as complete</label>
			<ul class="todo-list" id="todo-list">
				<!-- These are here just to show the structure of the list items -->
				<!-- List items should get the class `editing` when editing and `completed` when marked as completed -->
				<li class="completed">
					<div class="view">
						<input class="toggle" type="checkbox" checked>
						<label>Taste JavaScript</label>
						<button class="destroy"></button>
					</div>
					<input class="edit" value="Create a TodoMVC template">
				</li>
				<li>
					<div class="view">
						<input class="toggle" type="checkbox">
						<label>Buy a unicorn</label>
						<button class="destroy"></button>
					</div>
					<input class="edit" value="Rule the web">
				</li>
			</ul>
		</section>
		<!-- This footer should hidden by default and shown when there are todos -->
		<footer class="footer">
			<!-- This should be `0 items left` by default -->
			<span class="todo-count"><strong>0</strong> item left</span>
			<!-- Remove this if you don't implement routing -->
			<ul class="filters">
				<li>
					<a class="selected" href="#/">All</a>
				</li>
				<li>
					<a href="#/active">Active</a>
				</li>
				<li>
					<a href="#/completed">Completed</a>
				</li>
			</ul>
			<!-- Hidden if no completed items are left ↓ -->
			<button class="clear-completed">Clear completed</button>
		</footer>
	</section>
	<script src="/js/jquery.min.js"></script>
	<script src="/js/template-web.js"></script>
	<script src="/nprogress/nprogress.js"></script>
	<script type="text/html" id="taskTpl">
		{{each tasks}}
		<li>
			<div class="view">
				<input class="toggle" type="checkbox" checked>
				<label>{{$value.title}}</label>
				<button class="destroy"></button>
			</div>
			<input class="edit" value="Create a TodoMVC template">
		</li>
		{{/each}}
	</script>
	<script>
		let taskArr = []
		let taskBox = $('#todo-list')
		// 展示任务列表
		$.ajax({
			type: 'get',
			url: '/todo/task',
			success: function (response) {
				taskArr = response
				render()
			}
		})
		// 添加任务
		$('.new-todo').on('keyup', function (event) {
			if (event.keyCode == 13) {
				let taskName = $(this).val()
				if (taskName.trim().length == 0) {
					alert('请输入内容')
					return
				} else {
					$.ajax({
						type: 'post',
						url: '/todo/addTask',
						contentType: 'application/json',
						data: JSON.stringify({ title: taskName }),
						success: function (response) {
							console.log(response);
						}
					})
				}
			}
		})
		function render() {
			var html = template('taskTpl', { tasks: taskArr })
			taskBox.html(html)
		}
	</script>
</body>

</html>